<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2002 – Useless cat. Consider `cmd < file | ..` or `cmd file | ..` instead.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2002 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2002">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h1 id="useless-cat-consider-cmd--file---or-cmd-file---instead">Useless
cat. Consider <code>cmd &lt; file | ..</code> or
<code>cmd file | ..</code> instead.</h1>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2002.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="kw">|</span> <span class="fu">tr</span> <span class="st">&#39; &#39;</span> _ <span class="kw">|</span> <span class="fu">nl</span></span>
<span id="cb1-2"><a href="SC2002.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="kw">|</span> <span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span> <span class="bu">read</span> <span class="at">-r</span> <span class="va">i</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">${i</span><span class="op">%</span><span class="pp">?</span><span class="va">}</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2002.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="op">&lt;</span> file <span class="fu">tr</span> <span class="st">&#39; &#39;</span> _ <span class="kw">|</span> <span class="fu">nl</span>  </span>
<span id="cb2-2"><a href="SC2002.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span> <span class="bu">read</span> <span class="at">-r</span> <span class="va">i</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">${i</span><span class="op">%</span><span class="pp">?</span><span class="va">}</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span> <span class="op">&lt;</span> file</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>cat</code> is a tool for con"cat"enating files. Reading a
single file as input to a program is considered a <a
href="http://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat">Useless
Use Of Cat (UUOC)</a>.</p>
<p>It's more efficient and less roundabout to simply use redirection.
This is especially true for programs that can benefit from seekable
input, like <code>tail</code> or <code>tar</code>.</p>
<p>Many tools also accept optional filenames, e.g.
<code>grep -q foo file</code> instead of
<code>cat file | grep -q foo</code>.</p>
<h3 id="exceptions">Exceptions</h3>
<p>Pointing out UUOC is a long standing shell programming tradition, and
removing them from a short-lived pipeline in a loop can speed it up by
2x. However, it's not necessarily a good use of time in practice, and
rarely affects correctness. <a href="Ignore.html">Ignore</a> as you see
fit.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


